package com.moduls.interfaceFunction.core;

import com.moduls.interfaceFunction.model.User;

import java.time.LocalDateTime;
import java.util.HashMap;

public class CacheAuthorService implements AuthorService {

      HashMap<String,LocalDateTime> usermap=new HashMap<String,LocalDateTime>();
    @Override
    public String login(User user) {

        if(user.getName().equals("test")&&user.getPassword().equals("test"))
        {
            String token = JWT.sign(user, 30L * 24L * 3600L * 1000L);
            usermap.put(token,LocalDateTime.now());
             return token;
        }
        else
        {
            throw new MyException("用户名或密码错误!");
        }
    }

    @Override
    public boolean validate(String token) {

        if(!usermap.containsKey(token))
        {
            throw new MyException("用户名或密码错误!");
        }
        User user = JWT.unsign(token, User.class);
        if(user==null)
        {
            return false;
        }

        return true;
    }

    @Override
    public boolean logout(String token) {
        usermap.remove(token);
        return true;
    }
}
